package com.neuedu.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;

@Aspect
public class MyAdvice {



    //定义一个切点，insert方法
    @Pointcut("execution(* com.neuedu.spring.aop.service.*.insert(..))")
    public void pointcut(){}



//    //定义一个需要在insert方法之前指定的逻辑
//    @Before("pointcut()")
//    public void before(){
//        System.out.println("开启事务..代表了一些逻辑");
//    }



    @After("pointcut()")
    public void after(){
        System.out.println("关闭事务，提交事务");
    }



//    @AfterThrowing(value="pointcut()" ,throwing="throwable")
//    public void afterThrowing(Throwable throwable){
//        System.out.println("MyAdvice.afterThrowing"+throwable);
//    }


    @Around(value="pointcut()" )
    public void around(ProceedingJoinPoint joinPoint){


        System.out.println("Signature:"+joinPoint.getSignature().toLongString());
        System.out.println("getThis:\t"+joinPoint.getThis());
        System.out.println("getTarget:\t"+joinPoint.getTarget());
        //方法之前
        System.out.println("aroundBefore");
        try {
            joinPoint.proceed(); //调用原始的方法
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        //方法之
        System.out.println("arondAfter");
    }

    @AfterReturning(value="pointcut()" ,returning="returnObj")
    public void afterReturning(Object returnObj){
        System.out.println("MyAdvice.afterReturning"+returnObj);
    }



}
